<template>
    <el-select v-model="state.modelValue"
               :placeholder="props.placeholder"
               :disabled="props.disabled"
               :clearable="props.clearable"
               @change="proxy.$emit('change', $event)"
               @clear="proxy.$emit('clear')"
    >
        <el-option v-for="item in state.options" :key="item.value" :label="item.label" :value="item.value"/>
    </el-select>
</template>

<script lang="ts" setup>
    import {
        reactive,
        onMounted,
        watch,
        getCurrentInstance,
        ref,
        defineExpose,
    } from "vue";
    import {ISelectOption} from "../interface";
    import {Session} from "/@/utils/storage";

    const {proxy}: any = getCurrentInstance();

    const props = defineProps({
        modelValue: {
            type: String,
            default: ''
        },
        placeholder: {
            type: String,
            default: '请选择',
        },
        disabled: {
            type: Boolean,
            default: false
        },
        typeCode: {
            type: String,
            default: ''
        },
        clearable: {
            type: Boolean,
            default: true
        }
    });
    const state = reactive({
        modelValue: '',
        options: [] as Array<ISelectOption> | null
    });

    const module = {
        init: () => {
            if (module.getSelectOption() != null) {
                state.options = module.getSelectOption();
                state.modelValue = state.options[0].value;
            } else {
                proxy.$wigem.alert.warning("未找到该分类报告期数据！")
            }
        },
        getSelectOption: (): Array<ISelectOption> | null => {
            let data = Session.get("overallSituationReport");
            if (props.typeCode != '') {
                let result: Array<ISelectOption> = [];
                data.forEach((item: any) => {
                    if (item.tbflag == props.typeCode) {
                        result.push({
                            label: item.reportname,
                            value: item.id
                        })
                    }
                })
                return result;
            }
            return null;
        },
        reset:()=>{
            state.modelValue = state.options[0].value;
        }
    }
    onMounted(() => {
        module.init();
    });
    watch(
        () => state.modelValue,
        () => {
            proxy.$emit('update:modelValue', state.modelValue)
        }
    )
    defineExpose({
        module
    })
</script>

<style lang="scss" scoped>

</style>
